<template>
  <div class="container">
    <PropTable
      :columns="column"
      :data="list"
      :total="total"
      @reset="reset"
      @onSubmit="onSubmit"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
      @exportExcelAction="exportExcelAction"
      permissionPrefix="system:review"
    >
      <!-- <template v-slot:projectStatus="scope">{{
        stateTransition(scope.row.projectStatus)
      }} </template> -->

      <template v-slot:projectStatus="scope">
        <div v-if="!useEmpty(scope.row.projectStatus)">
          {{ stateTransition(scope.row.projectStatus) }}
        </div>
      </template>

      <template #otherSearch>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="creatTime"
            type="daterange"
            range-separator="至"
            start-placeholder="请选择"
            end-placeholder="请选择"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </template>

      <template v-slot:reviewTime="scope">{{
        formatDate(scope.row.reviewTime)
      }}</template>
      <template v-slot:operation="scope">
        <div
          style="
            width: 100px;
            margin: 0 auto;
            display: flex;
            justify-content: flex-end;
          "
        >
          <el-button
            link
            type="primary"
            icon="Notebook"
            @click="Review(scope.row)"
            v-if="userStore.rolescode!=15"
          >
            编辑
          </el-button>
  

          <el-button
            v-if="scope.row.projectStatus !== '0' || userStore.rolescode==15"
            link
            type="primary"
            icon="Message"
            @click="ReviewDetail(scope.row)"
            >详情
          </el-button>


          <el-button
            v-if="userStore.userId=='1'"
            type="primary"
            link
            icon="Edit"
            @click="deleteReview(scope.row)" >
            删除
          </el-button>
        </div>
      </template>
    </PropTable>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import PropTable from "@/components/Table/PropTable/index.vue";
import { useRouter } from "vue-router";
import { exportExcel } from "@/utils/exprotExcel";
import { reviewList, withdrawApi,deleteReviewData } from "@/api/project/review";
import { formatDate } from "@/utils/index";
import { useEmpty } from "@/utils/index";
import { ElMessageBox, ElMessage } from "element-plus";
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const router = useRouter();
const { proxy } = getCurrentInstance();
const loading = ref(false);
const creatTime = ref();
const reviewTimeA = ref();
const reviewEndTime = ref();
let statusList = [
  {
    value: "0",
    label: "复盘驳回",
  },
  {
    value: "1",
    label: "已完成",
  },
  {
    value: "2",
    label: "复盘中",
  },
  {
    value: "3",
    label: "复盘撤回",
  },
];
const column = [
  {
    name: "projectName",
    label: "项目名称",
    inSearch: true,
    valueType: "input",
  },
  {
    name: "reviewTime",
    label: "复盘日期",
  },
  { name: "reviewPerson", label: "复盘发起人" },
  { name: "operation", slot: true, fixed: "right", width: 300, label: "操作" },
];

// 处理项目状态
const stateTransition = (row) => {
  let transitions = statusList.find((item) => item.value === row);
  return transitions ? transitions.label : "";
};

// 复盘
const Review = (row) => {
  router.push({
    path: "/myProject/review/again",
    query: { id: row.reviewId, name: "2" },
  });
};
// 详情
const ReviewDetail = (row) => {
  router.push({
    path: "/myProject/review/again",
    query: { id: row.reviewId, name: "3" },
  });
};
// 导出
function exportExcelAction(val) {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  };
  if (creatTime.value && creatTime.value[0]) {
    reviewTimeA.value = creatTime.value[0];
  }
  if (creatTime.value && creatTime.value[1]) {
    reviewEndTime.value = creatTime.value[1];
  }

  if (val) {
    for (let key in val) {
      if (val[key]) {
        params[key] = val[key];
      }
    }
  }

  proxy.download(
    "/system/review/export",
    params,
    `项目复盘_${
      new Date().getFullYear() +
      "-" +
      new Date().getMonth() +
      1 +
      "-" +
      new Date().getDate()
    }.xlsx`
  );
}
const list = ref([]);
let pageNum = ref(1);
let pageSize = ref(10);
let total = ref(0);
const getReviewList = async () => {
  if (creatTime.value && creatTime.value[0]) {
    reviewTimeA.value = creatTime.value[0];
  }
  if (creatTime.value && creatTime.value[1]) {
    reviewEndTime.value = creatTime.value[1];
  }
  if (creatTime.value === null) {
    reviewTimeA.value = "";
    reviewEndTime.value = "";
  }
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    reviewTimeA:reviewTimeA.value,
    reviewEndTime:reviewEndTime.value
  };
  // console.log(paramsSubmit,'----paramsSubmit---')

  if (paramsSubmit) {
    for (let key in paramsSubmit) {
      if (paramsSubmit[key]) {
        params[key] = paramsSubmit[key];
      }
    }
  }
  const res = await reviewList(params);
  console.log(res);
  if (res.code === 200 && res.rows.length >= 0) {
    list.value = res.rows;
    total.value = res.total;
  } else {
    loading.value = false;
  }
  console.log(res);
};

// 重置
const reset = (val) => {
  val = "";
  creatTime.value=''
  reviewTimeA.value=''
  reviewEndTime.value=''
  getReviewList();
};
let paramsSubmit;
// 查询
const onSubmit = (val) => {
  val.reviewTime = formatDate(val.reviewTime);
  paramsSubmit = val;
  getReviewList();
};
// 切换每页条数
const handleSizeChange = (valueSise) => {
  console.log(valueSise);
  pageSize.value = valueSise;
  getReviewList();
};
// 切换页码
const handleCurrentChange = (valuePage) => {
  console.log(valuePage);
  pageNum.value = valuePage;
  getReviewList();
};

const handeleRevoke = (row) => {
  ElMessageBox.confirm("确定要撤回吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      if (row) {
        const result = await withdrawApi(row);
        if (result.code === 200) {
          // 删除成功的提示
          ElMessage.success(result.msg);
          getReviewList();
        }
      }
    })
    .catch((err) => {
      // 用户点击取消执行的操作
      ElMessage.error("取消");
      // console.log(err);
    });
};


//删除
const deleteReview = async (val)=>{
   ElMessageBox.confirm("确定删除该项目复盘吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    let response = await deleteReviewData(val.reviewId);
    if (response.code === 200) {
      // 删除成功的提示
        ElMessage.success(response.msg);
        getReviewList();
    } else {
      ElMessage.error(response.msg);
    }
  });
}


onMounted(async () => {
  await getReviewList();
  setTimeout(() => {
    loading.value = false;
  }, 500);
});
</script>
<style scoped lang="scss">
.container {
  padding: 20px;
}
</style>
